AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

function ENT:Initialize()   
self.Entity:SetModel( "models/props_c17/utilityconnecter006c.mdl" ) 	
self.Entity:PhysicsInit( SOLID_VPHYSICS )      -- Make us work with physics,  	
self.Entity:SetMoveType( MOVETYPE_VPHYSICS )   --after all, gmod is a physics  	
self.Entity:SetSolid( SOLID_VPHYSICS )        -- CHEESECAKE!    >:3            

 local phys = self.Entity:GetPhysicsObject()  	
	if (phys:IsValid()) then  		
		phys:Wake() 
	end 
self.Inputs = Wire_CreateInputs(self, { "Active" } )
self.Outputs = Wire_CreateOutputs( self.Entity, { "Can Fire"})
self.Done = false
self.active = false
self.EndTime = 0
self.Running = false

self.ParticleSystem = ents.Create( "info_particle_system" )
	self.ParticleSystem:SetPos(self.Entity:GetPos() + self.Entity:GetUp() * 20)
	self.ParticleSystem:SetKeyValue( "effect_name", "electrical_arc_01_system")
	self.ParticleSystem:Spawn()
	self.ParticleSystem:Activate()
	self.ParticleSystem:SetParent(self.Entity)
	self.ParticleSystem:Fire("Stop","", 0)
end   

function ENT:SpawnFunction( ply, tr)
	
	if ( !tr.Hit ) then return end
	local SpawnPos = tr.HitPos + tr.HitNormal * 60
	
	
	local ent = ents.Create( "emp_cannon" )
		ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	
	
	
	return ent

end

function ENT:Think()
 
	if (self.EndTime < CurTime()) then
		Wire_TriggerOutput(self.Entity, "Can Fire", 1)
	else
		Wire_TriggerOutput(self.Entity, "Can Fire", 0)
	end
 
	if self.active then
	
		if not self.Done then
			self.List = self:FindinConeLD( self.Entity:GetPos(), self.Entity:GetUp(), 700, 20 )
			self.EndTime = CurTime() + 10
			self.Done = true
			self.Running = true
			local effectdata = EffectData()
				effectdata:SetOrigin( self.Entity:GetPos() + self.Entity:GetUp() )
				effectdata:SetNormal( self.Entity:GetUp():Normalize() )
					util.Effect( "thruster_ring_grow", effectdata )
			self.ParticleSystem:Fire("Start","", 0)
			self.ParticleSystem:Fire("Stop","", 5)
			for _, v in pairs(self.List) do
				if v.Inputs or v.Outputs then
					local effectdata = EffectData() 
					effectdata:SetStart( v:GetPos() )
					effectdata:SetOrigin( v:GetPos() )
					effectdata:SetScale( 10 )
					effectdata:SetMagnitude( 10 )
					effectdata:SetEntity(v)
					util.Effect( "TeslaHitBoxes", effectdata ) --YAY lighning 
				end
			end	
		end
	end
	if self.Running and CurTime() then
		if CurTime() < self.EndTime then
			for _,v in pairs(self.List) do
				self:EMP(v, true)
			end
	
		else
			for _,v in pairs(self.List) do
			self:EMP(v, false)
			end
			self.Done = false
			self.Running = false
		end
	end	
		self.Entity:NextThink( CurTime() + .25 )	
	
end

function ENT:TriggerInput(iname, value)
	if ( value > 0 && iname == "Active" ) then
		self.active = true
	else
		self.active = false
	end
end

function ENT:EMP( entity , on ) --emp core Thank you AlgorithmX2
if (!entity or !entity:IsValid() ) then return end 
	if (entity) and (entity.Inputs) and type(entity.Inputs) == 'table' then
		for k,v in pairs(entity.Inputs) do
			if v.Type == "NORMAL" then
				if (entity.TriggerInput) then
					if on then
						entity:TriggerInput( k , entity.Inputs[ k ].Value * 0 )
					else
						entity:TriggerInput( k , entity.Inputs[ k ].Value )
					end
				end
			elseif v.Type == "VECTOR" then
				if (entity.TriggerInput) then
					if on then
						entity:TriggerInput( k , entity.Inputs[ k ].Value * Vector(0,0,0) )
					else
						entity:TriggerInput( k , entity.Inputs[ k ].Value )
					end
				end
			end
		end
end
end

function ENT:FindinConeLD(pos, dir, dist, ang)
    local ent = ents.GetAll() 
    for k,v in pairs (ent) do 
        local targetVec = (v:GetPos() - pos):GetNormal()
		local angle = math.acos(dir:DotProduct(targetVec))
		local range = pos:Distance(v:GetPos())
        if math.Rad2Deg(angle) > ang or range > dist then 
            ent[k] = nil 
        end 
    end 
    return ent 
end